#pragma once

#include "Object/Object.h"
#include "Math/Vec2.h"  
#include "Containers/ScriptDelegateImplement.h"
#include "EditorSplitableLayoutViewModel.m.h"


class REditorSplitableLayoutModel;

RCLASS()
class REditorSplitableLayoutViewModel : public RObject
{
    ROBJECT

public:
    void BindModel(TSharedObjectPtr<REditorSplitableLayoutModel> InModel );

    //创建ItemViewModel的函数
    //需要在绑定Model之前设置
    TScriptDelegate<
        TSharedObjectPtr<RObject> /*创建好的ItemVIewModel*/
        (TSharedObjectPtr<RObject> /*ItemModel*/ )
    > OnBindItemViewModel;

    //通知视图布局发生变化
    TDelegate<void> OnLayoutChanged;

    int64_t GetNodeNum() const;
    int64_t GetNodeSpliterNum(int64_t NodeIndex) const;
    bool HasSubNode(int64_t NodeIndex) const;
    TVector<int64_t> GetSubNodeIndices(int64_t NodeIndex) const;
    float GetSubNodeSize(int64_t NodeIndex,int64_t SubNodeIndex) const;
    void SetSubNodeSize(int64_t NodeIndex,int64_t SubNodeIndex,float Size);
    bool IsHorizontal(int64_t NodeIndex);

    float GetSpliterWidth() const { return 1.0f; }
    float GetSpliterMouseWidth() const { return 4.0f; }

    void OnSpliterDragStarted(int64_t NodeIndex,int64_t SpliterIndex);
    void OnSpliterDragMoved(int64_t NodeIndex,int64_t SpliterIndex,const SVec2& Delta);
    void OnSpliterDragEnded(int64_t NodeIndex,int64_t SpliterIndex);

    void OnEnterSpliter(int64_t NodeIndex,int64_t SpliterIndex);
    void OnExitSpliter(int64_t NodeIndex,int64_t SpliterIndex);
    bool IsSpliterHighlighted(int64_t NodeIndex,int64_t SpliterIndex) const;
protected:
    TWeakObjectPtr<REditorSplitableLayoutModel> Model;

    int64_t DraggingSpliterNodeIndex=-1;
    int64_t DraggingSpliterIndex=-1;
    //开始拖拽时子节点大小
    TVector<float> SubNodeSizesAtDraggingStart;


    int64_t EnteredSpliterIndex=-1;
    int64_t EnteredSpliterNodeIndex=-1;

public:
    TSharedObjectPtr<RObject> GetItemViewModel(int64_t Index) const;

protected:
    //叶子节点内部模型的视图模型
    //这个数组和节点一一对应
    RFIELD()
    TVector<TSharedObjectPtr<RObject>> ItemViewModels;

    //叶子节点内部模型,用于在布局变化时更新视图模型
    //这个数组和节点一一对应
    RFIELD()
    TVector<TSharedObjectPtr<RObject>> ItemModels;

};
